package com.bestv.search.management.command;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.model.TopNCache;
import com.bestv.search.common.service.InfoManager;
import com.bestv.search.common.service.SearchKeywordHistoryManager;
import com.bestv.search.common.service.TopNCacheManager;
import com.bestv.search.common.util.StringConst;

import java.util.List;

/**
 * @author Michael Wang
 */
public class TopNCacheCommand {

    private final String[] allCategorys = new String[]{"全部", "电影", "电视剧", "少儿", "体育", "凤凰", "娱乐", "新闻", "时尚生活", "法治",
            "纪实", "财经", "音乐"};

    private SearchKeywordHistoryManager searchKeywordHistoryManager;
    private TopNCacheManager topNCacheManager;
    private InfoManager infoManager;

    public void updateTopNCache(String dsKey) {
        DataSourceKey key = DataSourceKeyUtil.convert(dsKey);

        // Updates keywords topNCache
        updateTopNCacheKeywords(key);

        // Updates infos topNCache
        updateTopNCacheInfos(key);
    }

    private void updateTopNCacheKeywords(DataSourceKey key) {
        // Update daily TopNCache
        List<TopNCache> list = searchKeywordHistoryManager.getKeywordTopNCache(key, StringConst.DAILY);
        topNCacheManager.saveOrUpdate(key, list);

        // Update week TopNCache
        list = searchKeywordHistoryManager.getKeywordTopNCache(key, StringConst.WEEKLY);
        topNCacheManager.saveOrUpdate(key, list);

        // Update month TopNCache
        list = searchKeywordHistoryManager.getKeywordTopNCache(key, StringConst.MONTHLY);
        topNCacheManager.saveOrUpdate(key, list);

        // Update quarter TopNCache
        list = searchKeywordHistoryManager.getKeywordTopNCache(key, StringConst.QUARTER);
        topNCacheManager.saveOrUpdate(key, list);

        // Update total TopNCache
        list = searchKeywordHistoryManager.getKeywordTopNCache(key, StringConst.TOTAL);
        topNCacheManager.saveOrUpdate(key, list);
    }

    private void updateTopNCacheInfos(DataSourceKey key) {
        // Update Media code hit cache for get TopNCache
        topNCacheManager.updateMediaCodeHitCache(key, StringConst.DAILY);
        topNCacheManager.updateMediaCodeHitCache(key, StringConst.WEEKLY);
        topNCacheManager.updateMediaCodeHitCache(key, StringConst.MONTHLY);
        topNCacheManager.updateMediaCodeHitCache(key, StringConst.QUARTER);

        for (String category : allCategorys) {
            // Update daily TopNCache
            List<TopNCache> list = infoManager.getInfosTopNCache(key, category, StringConst.DAILY);
            topNCacheManager.saveOrUpdate(key, list);

            // Update week TopNCache
            list = infoManager.getInfosTopNCache(key, category, StringConst.WEEKLY);
            topNCacheManager.saveOrUpdate(key, list);

            // Update month TopNCache
            list = infoManager.getInfosTopNCache(key, category, StringConst.MONTHLY);
            topNCacheManager.saveOrUpdate(key, list);

            // Update quarter TopNCache
            list = infoManager.getInfosTopNCache(key, category, StringConst.QUARTER);
            topNCacheManager.saveOrUpdate(key, list);

            // Update total TopNCache
            list = infoManager.getInfosTopNCache(key, category, StringConst.TOTAL);
            topNCacheManager.saveOrUpdate(key, list);
        }
    }

    public SearchKeywordHistoryManager getSearchKeywordHistoryManager() {
        return searchKeywordHistoryManager;
    }

    public void setSearchKeywordHistoryManager(SearchKeywordHistoryManager searchKeywordHistoryManager) {
        this.searchKeywordHistoryManager = searchKeywordHistoryManager;
    }

    public TopNCacheManager getTopNCacheManager() {
        return topNCacheManager;
    }

    public void setTopNCacheManager(TopNCacheManager topNCacheManager) {
        this.topNCacheManager = topNCacheManager;
    }

    public InfoManager getInfoManager() {
        return infoManager;
    }

    public void setInfoManager(InfoManager infoManager) {
        this.infoManager = infoManager;
    }
}
